package com.smalls.newvideotwo.auth;

import android.os.AsyncTask;

import com.smalls.newvideotwo.BuildConfig;
import com.smalls.newvideotwo.NewDTVApp;
import com.smalls.newvideotwo.adapter.RowAdapter;
import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.utils.AppUtils;
import com.smalls.newvideotwo.utils.Constant;
import com.uking.helper.VKPortalProxy;

/**
 * Created by cindy on 2018/4/14.
 */

public class AAAActivateTask extends AsyncTask<String, Void, AAAActivateResult> {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "RequestAuthDataTask":"RequestAuthDataTask";
    private OnResultListener onResultListener;
    private String m3A = null;

    public interface OnResultListener {
        void onResult(AAAActivateResult uid);
    }

    public void set3A(String aaa) {
        m3A = aaa;
    }

    public AAAActivateTask setOnResultListener(OnResultListener onResultListener) {
        this.onResultListener = onResultListener;
        return this;
    }

    @Override
    protected AAAActivateResult doInBackground(String... voids) {
        if(m3A==null) {
            return null;
        }
        LogUtil.d(TAG, "aaa domain1="+ m3A);
        LogUtil.d(TAG, "packageName=" + AppUtils.getPackageName(NewDTVApp.getApp()));
        String code = "";
        if(voids!=null&&voids.length>0) {
            code = voids[0];
        } else {
            code ="981049612263669760";
        }
        LogUtil.d(TAG, "aaa code="+code);
        String result = new VKPortalProxy(AppUtils.getVKAddrSharedPreferences(NewDTVApp.getApp())).activateAuth(m3A,
                AppUtils.getPackageName(NewDTVApp.getApp()),
                code);
        LogUtil.d(TAG,"result="+result);
        if(result!=null&&result.length()>0) {
            AAAActivateResult aaaActivateResult = new AAAActivateResult();
            String[] params = result.split("&");
            for(int i=0;i<params.length;i++) {
                if(isCancelled()){
                    break;
                }
                if(params[i].regionMatches(false,0,"CODE",0,4)) {
                    aaaActivateResult.CODE = params[i].substring("CODE=".length());
                    LogUtil.d(TAG,"CODE="+aaaActivateResult.CODE);
                } else if(params[i].regionMatches(false,0,"INFO",0,4)) {
                    aaaActivateResult.INFO = params[i].substring("INFO=".length());
                    LogUtil.d(TAG,"INFO="+aaaActivateResult.INFO);
                } else if(params[i].regionMatches(false,0,"USERID",0,5)) {
                    aaaActivateResult.USERID = params[i].substring("USERID=".length());
                    LogUtil.d(TAG,"USERID="+aaaActivateResult.USERID);
                } else if(params[i].regionMatches(false,0,"ACTIVATETM",0,4)) {
                    aaaActivateResult.ACTIVATETM = params[i].substring("ACTIVATETM=".length());
                    LogUtil.d(TAG,"ACTIVATETM="+aaaActivateResult.ACTIVATETM);
                }
            }
            return aaaActivateResult;
        } else {
            return null;
        }
    }

    protected void onPostExecute(AAAActivateResult result) {
        super.onPostExecute(result);
        if(isCancelled()){
            return;
        }
        if (this.onResultListener != null) {
            this.onResultListener.onResult(result);
        }
    }
}
